<template>
  <div id="followed">
    <UserItem
      :userInfo="{ ...user, isFollow: true }"
      v-for="user in focusList.data"
      :key="user.focus_user_id"
      @queryData="queryData"
    />
    <div class="pagination" v-if="focusList.count != 0">
      <el-pagination
        :page-size="pageSize"
        :pager-count="5"
        layout="prev, pager, next"
        :current-page="pageIndex"
        :total="focusList.count"
        background
        @prev-click="prevPage"
        @next-click="nextPage"
        @current-change="pageChange"
      >
      </el-pagination>
    </div>
    <div class="no-result" v-if="focusList.count == 0">
      <span>什么都没找到</span>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import UserItem from "@/components/UserItem";
export default {
  name: "Followed",
  data() {
    return {
      pageIndex: 1,
      pageSize: 10,
    };
  },
  components: {
    UserItem,
  },
  mounted() {
    this.$store.dispatch("focusList", {
      pageIndex: "1",
      pageSize: "10",
    });
  },
  computed: {
    ...mapState({
      focusList: (store) => store.user.focusList,
    }),
  },
  methods: {
    // 重新获取数据
    queryData() {
      this.$store.dispatch("focusList", {
        pageIndex: "1",
        pageSize: "10",
      });
    },

    // 上一页
    prevPage() {
      this.pageIndex -= 1;
      this.$store.dispatch("focusList", {
        pageIndex: this.pageIndex.toString(),
        pageSize: this.pageSize.toString(),
      });
    },
    // 下一页
    nextPage() {
      this.pageIndex += 1;
      this.$store.dispatch("focusList", {
        pageIndex: this.pageIndex.toString(),
        pageSize: this.pageSize.toString(),
      });
    },

    // 页数改变
    pageChange(page) {
      this.pageIndex = page

      this.$store.dispatch("focusList", {
        pageIndex: page.toString(),
        pageSize: this.pageSize.toString(),
      });
    },
  },
};
</script>

<style lang='less' scoped>
#followed {
  .pagination {
    display: flex;
    justify-content: center;
  }
  .no-result {
    height: 20vh;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #999;
  }
}
</style>